using UnityEngine;

[ExecuteInEditMode]
public class ControllerPlatformBalance : ControllerGeneric
{
	private float unit = 1f;

	public float lungime = 10f;

	public float latime = 1f;

	private float grosime = 0.5f;

	public GameObject cub;

	public GameObject nutRight;

	public GameObject nutLeft;

	public bool includeNuts;

	private float nutWidth = 0.131f;

	public float unghiMaxim = 15f;

	public bool autoMovement;

	public float autoMovementSpeed = 10f;

	public float startDelay;

	public override void OnLevelInPlace(Transform levelTransform)
	{
		base.OnLevelInPlace(levelTransform);
		if (levelTransform == base.transform.root)
		{
			PrepareForGame();
		}
	}

	public void PrepareForGame()
	{
		SetNames();
		SetParenting();
		PlatformBalance platformBalance = cub.AddComponent<PlatformBalance>();
		platformBalance.angleLimit = unghiMaxim;
		platformBalance.autoMovement = autoMovement;
		platformBalance.autoMovementSpeed = autoMovementSpeed;
		platformBalance.autoMovementStartDelay = startDelay;
		base.name = "will_destroy_" + base.name;
		if (Application.isPlaying)
		{
			Object.Destroy(base.gameObject);
		}
		else
		{
			Object.DestroyImmediate(base.gameObject);
		}
	}

	private void Update()
	{
		if (!Application.isEditor || Application.isPlaying)
		{
			return;
		}
		SetupRemoteControl();
		if (IsRemoteControlActive())
		{
			base.transform.position = remoteControl.transform.position;
			base.transform.rotation = remoteControl.transform.rotation;
			lungime = remoteControl.transform.localScale.z;
			latime = remoteControl.transform.localScale.x;
		}
		if (lungime < 1f)
		{
			lungime = 1f;
		}
		if (latime < 1f)
		{
			latime = 1f;
		}
		if (remoteControl != null)
		{
			if (remoteControl.manualScale)
			{
				remoteControl.scale = new Vector3(remoteControl.scale.x, grosime * unit, remoteControl.scale.z);
			}
			else
			{
				remoteControl.transform.localScale = new Vector3(latime, grosime * unit, lungime);
			}
		}
		Setup();
		SetupUVs();
	}

	private void Setup()
	{
		AssureParenting();
		if (cub != null)
		{
			cub.transform.localPosition = Vector3.zero;
			cub.transform.localEulerAngles = Vector3.right * -90f;
			if (includeNuts)
			{
				cub.transform.localScale = new Vector3((latime - 2f * nutWidth) / unit, (lungime - 0.25f) / unit, grosime / unit);
			}
			else
			{
				cub.transform.localScale = new Vector3(latime / unit, (lungime - 0.25f) / unit, grosime / unit);
			}
		}
		if (nutLeft != null)
		{
			if (includeNuts)
			{
				nutLeft.transform.localPosition = Vector3.left * (latime * 0.5f - nutWidth);
			}
			else
			{
				nutLeft.transform.localPosition = Vector3.left * (latime * 0.5f);
			}
		}
		if (nutRight != null)
		{
			if (includeNuts)
			{
				nutRight.transform.localPosition = Vector3.right * (latime * 0.5f - nutWidth);
			}
			else
			{
				nutRight.transform.localPosition = Vector3.right * (latime * 0.5f);
			}
		}
	}

	public void SetupRemoteControl()
	{
		if (remoteControl == null)
		{
			InitRemoteControl();
			remoteControl.gameObject.name = remoteControl.gameObject.name + base.name;
			remoteControl.gameObject.transform.position = base.transform.position;
			remoteControl.gameObject.transform.rotation = base.transform.rotation;
			remoteControl.gameObject.transform.localScale = new Vector3(latime, grosime * unit, lungime);
			remoteControl.gameObject.AddComponent<PresetsPlatformBalance>();
		}
	}

	public override void CopyFromRemoteControl(RemoteControl rc)
	{
		base.CopyFromRemoteControl(rc);
		CopyParentingFromRemoteControl(rc);
	}

	private void SetParenting()
	{
		for (int childCount = base.transform.childCount; childCount > 0; childCount = base.transform.childCount)
		{
			base.transform.GetChild(0).parent = base.transform.parent;
		}
	}

	private void SetNames()
	{
		cub.name = "Platform Balance";
		if (nutLeft != null)
		{
			nutLeft.name = "Platform Balance Nut Left";
		}
		if (nutRight != null)
		{
			nutRight.name = "Platform Balance Nut Right";
		}
	}

	public void SetupUVs()
	{
		Mesh mesh = cub.GetComponent<MeshFilter>().mesh;
		Vector2[] uv = mesh.uv;
		Vector3[] vertices = mesh.vertices;
		Vector3[] normals = mesh.normals;
		float num = 0.125f;
		float num2 = 0.25f;
		for (int i = 0; i < vertices.Length; i++)
		{
			if (!(normals[i] == Vector3.forward) && !(normals[i] == Vector3.back))
			{
				continue;
			}
			if (vertices[i].y == 0f)
			{
				if (vertices[i].x < 0f)
				{
					uv[i].x = 0.5f - latime * num;
					uv[i].y = 0f;
				}
				else
				{
					uv[i].x = 0.5f + latime * num;
					uv[i].y = 0f;
				}
			}
			else if (vertices[i].x < 0f)
			{
				uv[i].x = 0.5f - latime * num;
				uv[i].y = (0f - lungime) * num2;
			}
			else
			{
				uv[i].x = 0.5f + latime * num;
				uv[i].y = (0f - lungime) * num2;
			}
		}
		num = 0.25f;
		num2 = 1f;
		uv[8].x = 0.5f - latime * num;
		uv[8].y = 0f;
		uv[9].x = 0.5f + latime * num;
		uv[9].y = num2;
		uv[10].x = 0.5f + latime * num;
		uv[10].y = 0f;
		uv[11].x = 0.5f - latime * num;
		uv[11].y = num2;
		uv[12].x = 0.5f - latime * num;
		uv[12].y = 0f;
		uv[13].x = 0.5f + latime * num;
		uv[13].y = num2;
		uv[14].x = 0.5f + latime * num;
		uv[14].y = 0f;
		uv[15].x = 0.5f - latime * num;
		uv[15].y = num2;
		num = 0.25f;
		uv[16].x = 0f;
		uv[16].y = 0f;
		uv[17].x = lungime * num;
		uv[17].y = num2;
		uv[18].x = lungime * num;
		uv[18].y = 0f;
		uv[19].x = (0f - lungime) * num;
		uv[19].y = 0f;
		uv[20].x = 0f;
		uv[20].y = num2;
		uv[21].x = (0f - lungime) * num;
		uv[21].y = num2;
		uv[22].x = 0f;
		uv[22].y = 0f;
		uv[23].x = lungime * num;
		uv[23].y = num2;
		uv[24].x = lungime * num;
		uv[24].y = 0f;
		uv[25].x = (0f - lungime) * num;
		uv[25].y = 0f;
		uv[26].x = 0f;
		uv[26].y = num2;
		uv[27].x = (0f - lungime) * num;
		uv[27].y = num2;
		mesh.uv = uv;
	}
}
